package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.EmpleeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@Slf4j
@RequestMapping("category")
public class CategoryController {
    @Autowired
    CategoryService categoryService;


    @PostMapping
    @ResponseBody
    public R<String> save(@RequestBody Category category, HttpServletRequest request) {

        log.info("category {}" + category);

        categoryService.save(category);

        return R.success("添加分类成功");
    }

    @GetMapping("page")
    @ResponseBody
    //http://localhost:8080/category/page?page=1&pageSize=10
    public R<Page<Category>> page(Integer page, Integer pageSize) {
        Page<Category> pageInfo = categoryService.page(page, pageSize);
        return R.success(pageInfo);
    }

    @PutMapping
    @ResponseBody
    public R<String> update(@RequestBody Category category) {
        log.info("修改分类");
       // categoryService.update(category, null);
        categoryService.updateById(category);
        return R.success("修改分类成功");
    }

    @DeleteMapping
    @ResponseBody
    public R<String> delete(Long id) {
        categoryService.move(id);
        return R.success("删除分类成功");
    }

    @GetMapping("list")
    @ResponseBody
    public R<List<Category>> DishFlvor(Integer type) {

        LambdaQueryWrapper<Category> queryWrapper = Wrappers.lambdaQuery(Category.class).eq(type != null, Category::getType, type);
        List<Category> list = categoryService.list(queryWrapper);
        return R.success(list);
    }
}
